//外接订单详情
interface ICspOrderDetailViewModel extends ng.IScope {
    /**
     * 附件上传组件
     */
    attachmentUploader: any;
    /**
     * 商务专员下拉
     */
    businessOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 客服专员下拉
     */
    customerServiceOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 调度专员下拉
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
    /**
     * 货物类别下拉
     */
    goodsTypeDropDown: GoodsTypeListItemResponse[];
    /**
     * 货物下拉
     */
    goodsDropDown: GoodsListItemResponse[];
    /**
     * 结算单位下拉
     */
    settleDropDown: SettleListItemResponse[];
    /**
     * 紧急程度单位下拉
     */
    urgencyUnitDropDown: ValueListItemResponse[];
    /**
     * 货物数量单位下拉
     */
    goodsNumUnitDropDown: ValueListItemResponse[];
    /**
     * 吨位范围下拉
     */
    tonRangeDropDown: LinePriceListItemResponse[];
    /**
     * 应收单价单位下拉
     */
    priceUnitDropDown: ValueListItemResponse[];
    /**
     * 结算方式下拉
     */
    settleTypeDropDown: ValueListItemResponse[];
    /**
     * 是否含税下拉
     */
    includeTaxDropDown: ValueListItemResponse[];
    /**
     * 工程总量单位下拉
     */
    projectTotalUnitDropDown: ValueListItemResponse[];
    /**
     * 车型下拉
     */
    carTypeDropDown: ValueListItemResponse[];
    /**
     * 车长下拉
     */
    carLengthDropDown: ValueListItemResponse[];
    /**
     * 承运方式下拉
     */
    carriageWayDropDown: ValueListItemResponse[];
    /**
     * 订单编号
     */
    orderId: string;
    /**
     * 紧急程度
     */
    urgency: string;
    /**
     * 紧急程度单位
     */
    urgencyUnit: string;
    /**
     * 计划专员
     */
    planOfficer: string;
    /**
     * 商务专员
     */
    businessOfficer: EmployeeListItemResponse;
    /**
     * 商务专员id
     */
    businessOfficerId: string;
    /**
     * 客服专员
     */
    customerServiceOfficer: EmployeeListItemResponse;
    /**
     * 客服专员id
     */
    customerServiceOfficerId: string;
    /**
     * 调度专员
     */
    dispatchOfficer: EmployeeListItemResponse;
    /**
     * 调度专员id
     */
    dispatchOfficerId: string;
    /**
     * 客户编号
     */
    clientId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 发货计划人编号
     */
    consignorId: string;
    /**
     * 发货计划人名称
     */
    consignorName: string;
    /**
     * 发货计划人号码
     */
    consignorPhone: string;
    /**
     * 发货内容
     */
    shipPriceContent: string;
    /**
     * 中转地
     */
    viaList: viaAddressList[];
    /**
     * 车型
     */
    carType: string;
    /**
     * 车长
     */
    carLength: string;
    /**
     * 承运方式
     */
    carriageWay: string;
    /**
     * 装车效果
     */
    loadingResult: string;
    /**
     * 发货地省编号
     */
    shipProvinceCode: string;
    /**
     * 发货地市编号
     */
    shipCityCode: string;
    /**
     * 发货地区编号
     */
    shipAreaCode: string;
    /**
     * 发货地详细地址
     */
    shipAddress: string;
    /**
     * 送货地省编号
     */
    deliverProvinceCode: string;
    /**
     * 送货地市编号
     */
    deliverCityCode: string;
    /**
     * 发货地区编号
     */
    deliverAreaCode: string;
    /**
     * 发货地详细地址
     */
    deliverAddress: string;
    /**
     * 中转地省名称
     */
    viaProvince: string;
    /**
     * 中转地市名称
     */
    viaCity: string;
    /**
     * 中转地区名称
     */
    viaArea: string;
    /**
     * 发货时间
     */
    deliveryTime: string;
    /**
     * 到货时间
     */
    arrivalTime: string;
    /**
     * 里程数
     */
    mileage: string;
    /**
     * 货物类型编号
     */
    goodsTypeId: string;
    /**
     * 货物类别
     */
    goodsTypeName: string;
    /**
     * 货物编号
     */
    goodsId: string;
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    quantityOfGoods: string;
    /**
     * 货物数量单位
     */
    goodsUnit: string;
    /**
     * 货物数量2
     */
    quantityOfGoodsTwo: string;
    /**
     * 货物数量2单位
     */
    goodsUnitTwo: string;
    /**
     * 吨位范围
     */
    tonnageRange: string;
    /**
     * 应收单价
     */
    receivablePrice: number;
    /**
     * 应收单价单位
     */
    receivablePriceUnit: string;
    /**
     * 应收总价
     */
    receivableTotal: string;
    /**
     * 结算方式
     */
    settleType: string;
    /**
     * 是否含税
     */
    includeTax: string;
    /**
     * 应收说明
     */
    receivableRemarks: string;
    /**
     * 结算单位编号
     */
    settleId: string;
    /**
     * 结算单位
     */
    settleName: string;
    /**
     * 工程编号
     */
    projectId: string;
    /**
     * 工程代号
     */
    projectCode: string;
    /**
     * 工程名称
     */
    projectName: string;
    /**
     * 发货单号
     */
    shipOrderId: string;
    /**
     * 收货单位编号
     */
    takeGoodsCompanyId: string;
    /**
     * 收货单位
     */
    takeGoodsCompanyName: string;
    /**
     * 收货人
     */
    consignee: string;
    /**
     * 收货人号码
     */
    consigneePhone: string;
    /**
     * 工程总量
     */
    projectTotal: string;
    /**
     * 工程总量单位
     */
    projectTotalUnit: string;
    /**
     * 总线路信息
     */
    cspOrderLine: ICspOrderLineViewModel[];
    /**
     * 子线路信息
     */
    cspChildOrderLine: ICspChildOrderLineViewModel[];

    /**
     * 改变货物类别获取货物
     */
    changeGoodsTypeEvent: () => void;

    /**
     * 改变货物获取吨位范围事件
     */
    changeGoodsGetTonRangeList(): void;

    /**
     * 改变吨位范围获取应收信息
     */
    changeTonRangeGetReceivableEvent(): void;

    /**
     * 客户单位自动补全事件
     */
    onClientAutoComplate: (value: string) => any;

    /**
     * 发货计划人自动补全事件
     */
    onConsignorAutoComplate: (value: string) => any;
    /**
     * 发货计划人选中事件
     */
    onConsignorSelected: (selected: angucomplete<CustomerRepresentativeListItemResponse>) => void;
    /**
     * 工程自动补全事件
     */
    onProjectAutoComplate: (value: string) => any;
    /**
     * 选中工程事件
     */
    onProjectSelected: (select: angucomplete<ProjectListItemResponse>) => void;
    /**
     * 起始地市list
     */
    shipCityList: lineAddress[];
    /**
     * 起始地区
     */
    shipAreaList: lineAddress[]
    /**
     * 目的地市list
     */
    deliverCityList: lineAddress[];
    /**
     * 目的地区
     */
    deliverAreaList: lineAddress[];
    /**
     * 中转地
     */
    viaListData: viaAddressList[];

    /**
     * 编辑总线路事件
     */
    editOrderLine: () => void;

    /**
     * 显示添加总线路按钮
     */
    showAddOrderBtn: boolean;
    /**
     * 显示添加子线路按钮
     */
    showAddChildOrderBtn: boolean;

    /**
     * 保存总线路事件
     */
    saveOrderLine: (type: boolean) => void;
    /**
     * 新增总线路事件
     */
    addOrderLine: (type: boolean) => void;
    /**
     * 删除总线路
     */
    deleteOrderLine: () => void;
    /**
     * 删除子线路
     */
    deleteChildOrderLine: (index: number) => void;
    /**
     * 编辑子线路事件
     */
    editChildOrderLine: (index: number) => void;

    /**
     * 保存子线路事件
     */
    saveChildOrderLine: () => void;

    /**
     * 添加中转地
     */
    addVia: () => void;

    /**
     * 新增子线路事件
     */
    addChildOrderLine: () => void;

    /**
     * 保存按钮
     */
    orderSave: () => void;
    /**
     * 是否禁用保存按钮
     */
    enabledSaveBtn: boolean;
    /**
     * 删除中转地
     */
    deleteVia(index: number): void;

    orderRemarks: string;

    /**
     * 返回
     */
    shipBack(): void
    /**
     * 基本信息验证
     */
    basicInfo: boolean;
    /**
     * 车辆信息
     */
    carInfo: boolean;
    /**
     * 线路货物应收信息
     */
    orderTotalForm: boolean;
    /**
     * 应收价格信息
     */
    orderChildrenForm: boolean;
    /**
     * 结算单位自动补全
     */
    onSettleAutoComplate: (value: string) => any;
    /**
     * 结算单位选中事件
     */
    onsettleSelected: (select: angucomplete<SettleListItemResponse>) => void;
    /**
     * 
     */
    reckoner: string;
    /**
     * 选择客户单位事件
     */
    onClientSelected: (selected: angucomplete<ClientListItemResponse>) => void;
    /**
     * 货物名称自动补全事件
     */
    onGoodsNameAutoComplate: (value: string) => any;
    /**
     * 货物名称选中事件
     */
    onGoodsNameSelected: (selected: angucomplete<GoodsListItemResponse>) => void;
    /**
     * 下载附件
     */
    handleDownLoad:(item:any)=>void;

    //客户订单编号
    /**
     * 12.1新增
     * 客户订单编号
     */
    clientOrderId:string;
    /**
     * 子询价编号  180104新增
     */
    inquiryChildId:string;
    
}
/**
 * csp总线路视图
 */
interface ICspOrderLineViewModel {
    /**
     * 目的地详情地址（省市区）
     */
    destinationAddress: string;

    /**
     * 中转地数据
     */
    viaListData: viaAddressList[];

    /**
     * 起始地详情地址（省市区）
     */
    originAddress: string;

    /**
     * 发货时间
     */
    deliveryTime: string;
    /**
     * 到货时间
     */
    arrivalTime: string;
    /**
     * 里程数
     */
    mileage: string;
    /**
     * 货物类别名称
     */
    goodsTypeName: string;
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    quantityOfGoods: string;
    /**
     * 货物数量单位str
     */
    goodsUnitStr: string;


    //总

    /**
     * 起始地省
     */
    originProvince: string;
    /**
     * 起始地市
     */
    originCity: string;
    /**
     * 起始地区
     */
    originCounty: string;
    /**
     * 起始地详情
     */
    originDetails: string;
    /**
     * 目的地省
     */
    destinationProvince: string;
    /**
     * 目的地市
     */
    destinationCity: string;
    /**
     * 目的地区
     */
    destinationCounty: string;
    /**
     * 目的地详情
     */
    destinationDetails: string;
    /**
     * 货物类别名称id
     */
    goodsTypeId: string;
    /**
     * 货物名称id
     */
    goodsId: string;
    /**
     * 吨位范围
     */
    tonnageRange: string;
    /**
     * 货物数量单位
     */
    goodsUnit: string;
    /**
     * 货物数量2
     */
    quantityOfGoodsTwo: string;
    /**
     * 货物数量2单位
     */
    goodsUnitTwo: string;


}

/**
 * csp子线路视图
 */
interface ICspChildOrderLineViewModel {
    /**
     * 序号
     */
    index: number;
    /**
     * 目的地详情地址（省市区）
     */
    destinationAddress: string;
    /**
     * 起始地详情地址（省市区）
     */
    originAddress: string;
    /**
     * 中转地数据
     */
    viaListData: { province: string, city: string, county: string, index?: number }[];

    /**
     * 货物类别名称
     */
    goodsTypeName: string;
    /**
     * 货物名称
     */
    goodsName: string;
    /**
     * 货物数量
     */
    quantityOfGoods: string;
    /**
     * 货物数量单位
     */
    goodsUnitStr: string;
    /**
     * 应收单价
     */
    receivablePrice: number;
    /**
     * 应收单价单位
     */
    receivablePriceUnitStr: string;
    /**
     * 应收总价
     */
    receivableTotal: string;
    /**
     * 结算单位名称
     */
    settle: string;



    //总
    /**
     * 起始地省
     */
    originProvince: string;
    /**
     * 起始地市
     */
    originCity: string;
    /**
     * 起始地区
     */
    originCounty: string;
    /**
     * 起始地详情
     */
    originDetails: string;
    /**
     * 目的地省
     */
    destinationProvince: string;
    /**
     * 目的地市
     */
    destinationCity: string;
    /**
     * 目的地区
     */
    destinationCounty: string;
    /**
     * 目的地详情
     */
    destinationDetails: string;

    /**
     * 货物类别名称id
     */
    goodsTypeId: string;
    /**
     * 货物名称id
     */
    goodsId: string;
    /**
     * 吨位范围
     */
    tonnageRange: string;
    /**
     * 货物数量单位
     */
    goodsUnit: string;
    /**
     * 货物数量2
     */
    quantityOfGoodsTwo: string;
    /**
     * 货物数量2单位
     */
    goodsUnitTwo: string;
    /**
     * 发货时间
     */
    deliveryTime: string;
    /**
     * 到货时间
     */
    arrivalTime: string;
    /**
     * 里程数
     */
    mileage: string;


    //--子
    /**
     * 应收单价单位
     */
    receivablePriceUnit: string;
    /**
     * 结算方式
     */
    settlementType: string;
    /**
     * 结算方式Str
     */
    settlementTypeStr: string;
    /**
     *  含税
     */
    includeTax: string;
    /**
     * 应收说明
     */
    receivableSummary: string;
    /**
     * 结算单位id
     */
    settleId: string;
    //----工程
    /**
     * 工程名称
     */
    projectName: string;
    /**
     * 工程id
     */
    projectId: string;
    /**
     * 工程代号
     */
    projectCode: string;
    /**
     * 工程总量
     */
    projectMax: string;
    /**
     * 工程总量单位
     */
    projectMaxUnit: string;
    /**
     * 收货单位
     */
    consigneeCompany: string;
    /**
     * 收货单位id
     */
    takeGoodsCompanyId: string;
    /**
     * 收货人
     */
    consignee: string;
    /**
     * 收货人号码
     */
    consigneePhone: string;
    /**
     * 发货单号
     */
    orderNumber: string;

}


class CspOrderDetailController {
    /**
     * 外接单id
     */
    cspOrderId: string;
    /**
     * 客服单位id
     */
    clientId: string;
    /**
     * 附件数组
     */
    uploaderList: attachmentList[];
    baseImageUrl: string;
    /**
     * 正在编辑的子线路序号
     */
    childOrderIndex: number = -1;

    constructor(private $scope: ICspOrderDetailViewModel, private cspOrderService: IcspOrderService, private areaService: IAreaService, private projectService: IProjectService, private customerRepresentativeService: ICustomerRepresentativeService, private clientService: IClientService, private linePriceService: ILinePriceService, private employeeService: IEmployeeService, private goodsTypeService: IGoodsTypeService,  private routeService: routeService, private $http: ng.IHttpService, private inquiryService: IInquiryService,
        private goodsService: IGoodsService, private settleService: ISettleService, private valueService: IValueService, private $ngBootbox: BootboxService,
        private orderService: IorderService, private $state: angular.ui.IStateService, private $location: ng.ILocationService, private commonService: ICommonService, private $q: ng.IQService,private ossUploaderService:ossUploaderService) {

        this.cspOrderId = this.$location.search().id;
        //附件配置
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        this.uploaderList = [];
        this.init();
    }

    init(): void {
        this.$scope.planOfficer = JSON.parse(window.localStorage.getItem("loginData")).realName;
        //附件组件
      
        const OssFile = this.ossUploaderService.getOssFile();
        this.$scope.attachmentUploader= new OssFile(this.$scope,this.$scope.attachmentUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});
        this.$scope.attachmentUploader.onSuccess=(it)=>{
            console.log('onsuccess',it);
            this.onSuccessItem( it.fileName,it.imgSrc,1,it._file)

        }

        //枚举
        this.$scope.businessOfficerDropDown = [];
        this.$scope.customerServiceOfficerDropDown = [];
        this.$scope.dispatchOfficerDropDown = [];
        this.$scope.goodsTypeDropDown = [];
        this.$scope.goodsDropDown = [];
        this.$scope.settleDropDown = [];
        this.$scope.urgencyUnitDropDown = [];
        this.$scope.goodsNumUnitDropDown = [];
        this.$scope.settleTypeDropDown = [];
        this.$scope.includeTaxDropDown = [];
        this.$scope.projectTotalUnitDropDown = [];
        this.$scope.carTypeDropDown = [];
        this.$scope.carLengthDropDown = [];
        this.$scope.carriageWayDropDown = [];
        this.$scope.tonRangeDropDown = [];



        this.getCspOrderDetail();

        this.loadDropDown();//加载枚举
        //按钮
        this.$scope.showAddChildOrderBtn = true;
        this.$scope.showAddOrderBtn = true;


        //change事件
        this.$scope.changeGoodsTypeEvent = this.changeGoodsTypeEvent;
        this.$scope.changeGoodsGetTonRangeList = this.changeGoodsGetTonRangeList;
        this.$scope.changeTonRangeGetReceivableEvent = this.changeTonRangeGetReceivableEvent;
        //自动补全事件
        this.$scope.onClientAutoComplate = this.onClientAutoComplate;
        this.$scope.onConsignorAutoComplate = this.onConsignorAutoComplate;
        this.$scope.onConsignorSelected = this.onConsignorSelected;
        this.$scope.onProjectAutoComplate = this.onProjectAutoComplate;
        this.$scope.onProjectSelected = this.onProjectSelected;
        this.$scope.onGoodsNameAutoComplate = this.onGoodsNameAutoComplate;
        this.$scope.onGoodsNameSelected = this.onGoodsNameSelected;

        this.$scope.cspChildOrderLine = [];
        this.$scope.cspOrderLine = [];
        this.$scope.viaListData = [];


        this.$scope.editOrderLine = this.editOrderLine;
        this.$scope.saveOrderLine = this.saveOrderLine;
        this.$scope.addOrderLine = this.addOrderLine;
        this.$scope.deleteOrderLine = this.deleteOrderLine;
        this.$scope.editChildOrderLine = this.editChildOrderLine;
        this.$scope.saveChildOrderLine = this.saveChildOrderLine;
        this.$scope.addChildOrderLine = this.addChildOrderLine;
        this.$scope.orderSave = this.orderSave;
        this.$scope.deleteChildOrderLine = this.deleteChildOrderLine;

        this.$scope.addVia = this.addVia;
        this.$scope.deleteVia = this.deleteVia;

        this.$scope.shipBack = this.shipBack;

        this.$scope.onsettleSelected = this.onsettleSelected;
        this.$scope.onSettleAutoComplate = this.onSettleAutoComplate;

        this.$scope.onClientSelected = this.onClientSelected;
        
        this.$scope.handleDownLoad =(item) =>{
            window.location.href= window.location.origin+ item.imgSrc
        }
    
    }

    /**
     * 获取外接订单详情
     */
    getCspOrderDetail = () => {
        this.cspOrderService.cspOrderDetail(this.cspOrderId).then(res => {
            let data = res.data;
            this.clientId = data.clientId;
            this.$scope.clientOrderId = data.clientOrderId;
            this.$scope.clientId = data.clientId;
            this.$scope.urgency = data.responseTime;
            this.$scope.urgencyUnit = data.responseTimeUnit;
            this.$scope.clientName = data.clientName;
            this.$scope.consignorName = data.consignorName;
            this.$scope.consignorPhone = data.consignorPhone;
            this.$scope.consignorId = data.consignorId;
            this.$scope.shipPriceContent = data.content;

            this.$scope.carType = data.vehicleType;
            this.$scope.carLength = data.carLength;
            this.$scope.carriageWay = data.carriageWay;
            this.$scope.loadingResult = data.loadingEffect;

            this.$scope.orderRemarks = data.remarks;

            this.loadGoodsTypeDropDown();
            this.loadSettleDropDown();

            //附件
            res.data.attachmentList.forEach((item, index) => {
                // this.commonService.showFileItem(item, this.$scope.attachmentUploader);
                this.$scope.attachmentUploader.addByAttach(item)
                this.uploaderList.push({
                    attachmentId: item.attachmentId,
                    attachmentName: item.attachmentName,
                    attachmentPath: item.attachmentPath,
                    attachmentTag: item.attachmentTag
                });
            });


            this.getOrderLine(data);

            //获取子单列表
            this.getChildOrderLine(data.items);

        });
    }

    /**
     * 获取总单列表
     */
    getOrderLine = (data: cspOrderDetailResponse) => {
        var newviaListData = [];
        data.viaAddressList.forEach((item, index) => {
            newviaListData.push({
                province: item.province,
                city: item.city,
                county: item.county,
                index: index + 1
            });
        })
        //获取总单列表
        this.$scope.cspOrderLine = [{
            //显示
            originAddress: data.originAddress,
            destinationAddress: data.destinationAddress,
            viaListData: newviaListData,
            deliveryTime: data.deliveryTime,
            arrivalTime: data.arrivalTime,
            mileage: data.mileage,
            goodsTypeName: data.goodsTypeName,
            goodsName: data.goodsName,
            quantityOfGoods: data.quantityOfGoods,
            goodsUnitStr: data.goodsUnitStr,

            //传给数据
            originProvince: data.originProvince,
            originCity: data.originCity,
            originCounty: data.originCounty,
            originDetails: data.originDetails,
            destinationProvince: data.destinationProvince,
            destinationCity: data.destinationCity,
            destinationCounty: data.destinationCounty,
            destinationDetails: data.destinationDetails,


            goodsTypeId: data.goodsTypeId,
            goodsId: data.goodsId,

            tonnageRange: data.tonnageRange,

            goodsUnit: data.goodsUnit,
            quantityOfGoodsTwo: data.quantityOfGoodsTwo,
            goodsUnitTwo: data.goodsUnitTwo,

        }];
    }

    /**
     * 编辑总单
     */
    editOrderLine = () => {
        let ol = this.$scope.cspOrderLine[0];
        let sc = this.$scope;
        this.$scope.showAddOrderBtn = false;

        this.loadSettleDropDown();
        this.loadGoodsTypeDropDown();

        this.$scope.viaListData = ol.viaListData.map((value) => value);

        sc.shipProvinceCode = ol.originProvince;
        sc.shipCityCode = ol.originCity;
        sc.shipAreaCode = ol.originCounty;
        sc.shipAddress = ol.originDetails;
        sc.deliverProvinceCode = ol.destinationProvince;
        sc.deliverCityCode = ol.destinationCity;
        sc.deliverAreaCode = ol.destinationCounty;
        sc.deliverAddress = ol.destinationDetails;

        this.getProvinceList();
        sc.deliveryTime = ol.deliveryTime;
        sc.arrivalTime = ol.arrivalTime;
        $("#dataTime").val(ol.deliveryTime);
        $("#dataTimeEnd").val(ol.arrivalTime);
        sc.mileage = ol.mileage;
        sc.goodsTypeId = ol.goodsTypeId;
        sc.goodsTypeName = ol.goodsTypeName;
        $("#goodsId_value").val(ol.goodsName);
        sc.goodsId = ol.goodsId;
        sc.goodsName = ol.goodsName;
        sc.quantityOfGoods = ol.quantityOfGoods;
        sc.goodsUnit = ol.goodsUnit;
        sc.quantityOfGoodsTwo = ol.quantityOfGoodsTwo;
        sc.goodsUnitTwo = ol.goodsUnitTwo;
        sc.tonnageRange = ol.tonnageRange;

        this.getTonRangeList();
    }


    /**
     * 获取子单列表
     */
    getChildOrderLine = (data: CspOrderChildList[]) => {
        data.forEach((item, index) => {
            var newViaData = [];
            //中转地
            item.viaAddressList.forEach((item, index) => {
                newViaData.push({
                    province: item.province,
                    city: item.city,
                    county: item.county,
                    index: index
                });
            });
            this.$scope.cspChildOrderLine.push({
                //视图
                index: index + 1,
                originAddress: item.originAddress,
                viaListData: newViaData,
                destinationAddress: item.destinationAddress,
                goodsTypeName: item.goodsTypeName,
                goodsName: item.goodsName,
                quantityOfGoods: item.quantityOfGoods,
                goodsUnitStr: item.goodsUnitStr,
                receivablePrice: item.receivablePrice,
                receivablePriceUnitStr: item.receivablePriceUnitStr,
                receivableTotal: item.receivableTotalPrice,
                settle: item.settle,

                //传给数据
                //--总
                originProvince: item.originProvince,
                originCity: item.originCity,
                originCounty: item.originCounty,
                originDetails: item.originDetails,
                destinationProvince: item.destinationProvince,
                destinationCity: item.destinationCity,
                destinationCounty: item.destinationCounty,
                destinationDetails: item.destinationDetails,

                deliveryTime: item.deliveryTime,
                arrivalTime: item.arrivalTime,
                mileage: item.mileage,

                goodsTypeId: item.goodsTypeId,
                goodsId: item.goodsId,
                tonnageRange: item.tonnageRange,
                goodsUnit: item.goodsUnit,
                quantityOfGoodsTwo: item.quantityOfGoodsTwo,
                goodsUnitTwo: item.goodsUnitTwo,

                //--子
                //----应收
                receivablePriceUnit: item.receivablePriceUnit,
                settlementType: item.settlementType,
                includeTax: item.includeTax,
                receivableSummary: item.receivableSummary,
                settleId: item.settleId,
                //----工程
                projectName: item.projectName,
                projectId: item.projectId,
                projectCode: item.projectCode,
                projectMax: item.projectMax,
                projectMaxUnit: item.projectMaxUnit,
                consigneeCompany: item.consigneeCompany,
                takeGoodsCompanyId: item.takeGoodsCompanyId,
                consignee: item.consignee,
                consigneePhone: item.consigneePhone,
                orderNumber: item.orderNumber,
                settlementTypeStr: item.settlementTypeStr,
            })
        });
    }



    /**
     * 编辑子单
     */
    editChildOrderLine = (index: number) => {
        this.$scope.showAddChildOrderBtn = false;
        if (index > this.$scope.cspChildOrderLine.length) {
            return;
        }
        var sc = this.$scope;
        var ol: ICspChildOrderLineViewModel;
        this.$scope.cspChildOrderLine.forEach((item, indexS) => {
            if (item.index === index) {
                ol = item;
                return false;
            }
        });

        //下拉
        this.loadSettleDropDown();
        this.loadGoodsTypeDropDown();

        this.getTonRangeList();

        this.childOrderIndex = index;
        var newViaList = [];
        ol.viaListData.forEach((item, index) => {
            newViaList.push(item)
        })
        this.$scope.viaListData = newViaList;
        sc.shipProvinceCode = ol.originProvince;
        sc.shipCityCode = ol.originCity;
        sc.shipAreaCode = ol.originCounty;
        sc.shipAddress = ol.originDetails;
        sc.deliverProvinceCode = ol.destinationProvince;
        sc.deliverCityCode = ol.destinationCity;
        sc.deliverAreaCode = ol.destinationCounty;
        sc.deliverAddress = ol.destinationDetails;
        this.getProvinceList();
        sc.arrivalTime = ol.arrivalTime;
        sc.deliveryTime = ol.deliveryTime;
        $("#dataTime").val(ol.deliveryTime)
        $("#dataTimeEnd").val(ol.arrivalTime)
        sc.mileage = ol.mileage;

        sc.goodsTypeId = ol.goodsTypeId;
        sc.goodsTypeName = ol.goodsTypeName;
        sc.goodsId = ol.goodsId;
        sc.goodsName = ol.goodsName;
        $("#goodsId_value").val(ol.goodsName)
        sc.quantityOfGoods = ol.quantityOfGoods;
        sc.goodsUnit = ol.goodsUnit;
        sc.tonnageRange = ol.tonnageRange;
        sc.quantityOfGoodsTwo = ol.quantityOfGoodsTwo;
        sc.goodsUnitTwo = ol.goodsUnitTwo;
        if (ol.receivablePrice != 0) {
            sc.receivablePrice = ol.receivablePrice;
        } else {
            sc.receivablePrice = ol.receivablePrice;
        }

        sc.receivablePriceUnit = ol.receivablePriceUnit;
        if (ol.receivableTotal != '0') {
            sc.receivableTotal = ol.receivableTotal;
        } else {
            sc.receivableTotal = ol.receivableTotal;
        }

        sc.settleType = ol.settlementType;
        sc.includeTax = ol.includeTax;
        sc.receivableRemarks = ol.receivableSummary;
        sc.settleId = ol.settleId;
        sc.reckoner = ol.settle;
        $("#reckoner_value").val(ol.settle);

        sc.settleName = ol.settle;
        sc.projectId = ol.projectId;
        sc.projectCode = ol.projectCode;
        // sc.projectName = ol.projectName;
        $("#project_value").val(ol.projectName);
        sc.shipOrderId = ol.orderNumber;
        sc.takeGoodsCompanyId = ol.takeGoodsCompanyId;
        sc.takeGoodsCompanyName = ol.consigneeCompany;
        sc.consignee = ol.consignee;
        sc.consigneePhone = ol.consigneePhone;
        sc.projectTotal = ol.projectMax;
        sc.projectTotalUnit = ol.projectMaxUnit;

    }


    /**
     * 保存总线路
     */
    saveOrderLine = (type: boolean) => {
        this.addOrderLine(type);
        this.$scope.showAddOrderBtn = true;
    }


    /**
     * 保存子线路
     */
    saveChildOrderLine = () => {
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        if (this.childOrderIndex > this.$scope.cspChildOrderLine.length || this.childOrderIndex < 0) {
            return;
        }

        var settle = $("#reckoner_value").val();
        if (this.$scope.settleId == "" || settle == "") {
            this.$ngBootbox.alert("请选择结算单位");
            return;
        }

        // if (this.$scope.shipProvinceCode == "-1" || this.$scope.shipCityCode == "-1" || this.$scope.shipAreaCode == "-1"
        //     || this.$scope.deliverProvinceCode == "-1" || this.$scope.deliverCityCode == "-1" || this.$scope.deliverAreaCode == "-1") {
        //     this.$ngBootbox.alert("请填写完整地址");
        //     return;
        // }
        //缺表单验证

        let sc = this.$scope;
        // let ol = this.$scope.childOrderLine[this.childOrderIndex - 1];

        var ol: ICspChildOrderLineViewModel;
        this.$scope.cspChildOrderLine.forEach((item, indexS) => {
            if (item.index === this.childOrderIndex) {
                ol = item;
                return false;
            }
        });

        var newViaList = [];
        this.$scope.viaListData.forEach((item, index) => {
            newViaList.push(item)
        })
        ol.viaListData = newViaList;
        ol.originProvince = sc.shipProvinceCode;
        ol.originCity = sc.shipCityCode;
        ol.originCounty = sc.shipAreaCode;
        ol.originDetails = sc.shipAddress;
        ol.destinationProvince = sc.deliverProvinceCode;
        ol.destinationCity = sc.deliverCityCode;
        ol.destinationCounty = sc.deliverAreaCode;
        ol.destinationDetails = sc.deliverAddress;


        ol.originAddress = $(".province option:selected")[0].innerHTML + $(".city option:selected")[0].innerHTML + $(".area option:selected")[0].innerHTML + sc.shipAddress;
        ol.destinationAddress = $(".province option:selected")[1].innerHTML + $(".city option:selected")[1].innerHTML + $(".area option:selected")[1].innerHTML + sc.deliverAddress;

        ol.goodsUnitStr = $("#goodsNumUnit option:selected").text();

        ol.tonnageRange = $("#tonRange option:selected").text();
        ol.settlementType = $("#settleType option:selected").text();
        ol.settle = $("#reckoner_value").val();

        ol.goodsTypeName = $("#goodsType option:selected").text();
        ol.goodsName =  $("#goodsId_value").val();

        ol.deliveryTime = $("#dataTime").val();
        ol.arrivalTime = $("#dataTimeEnd").val();

        ol.mileage = sc.mileage;
        ol.goodsTypeId = sc.goodsTypeId;

        ol.goodsId = sc.goodsId;

        ol.quantityOfGoods = sc.quantityOfGoods;
        ol.goodsUnit = sc.goodsUnit;
        ol.quantityOfGoodsTwo = sc.quantityOfGoodsTwo;
        ol.goodsUnitTwo = sc.goodsUnitTwo;
        ol.tonnageRange = sc.tonnageRange;
        ol.receivablePrice = sc.receivablePrice;
        ol.receivablePriceUnit = sc.receivablePriceUnit;
        ol.receivablePriceUnitStr = $("#receivablePriceUnit option:selected").text();
        ol.receivableTotal = sc.receivableTotal;
        ol.settlementType = sc.settleType;
        ol.includeTax = sc.includeTax;
        ol.receivableSummary = sc.receivableRemarks;
        ol.settleId = sc.settleId;

        ol.projectId = sc.projectId;
        ol.projectCode = sc.projectCode;
        ol.projectName = $("#project_value").val();
        ol.orderNumber = sc.shipOrderId;
        ol.takeGoodsCompanyId = sc.takeGoodsCompanyId;
        ol.consigneeCompany = sc.takeGoodsCompanyName;
        ol.consignee = sc.consignee;
        ol.consigneePhone = sc.consigneePhone;
        ol.projectMax = sc.projectTotal;
        ol.projectMaxUnit = sc.projectTotalUnit;

        this.$scope.showAddChildOrderBtn = true;
    }

    /**
     * 添加总线路
     */
    addOrderLine = (type) => {
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        if (!type) {
            if (this.$scope.cspOrderLine.length > 0) {
                this.$ngBootbox.alert("只能添加一条总线路信息");
                return;
            }
        }
        if (this.$scope.goodsId == "") { this.$ngBootbox.alert("请选择货物"); return; }
        if ($("#goodsId option:selected").text() == "请选择") { this.$ngBootbox.alert("请填写货物名称"); return; };
        let sc = this.$scope;

        //获取总单列表
        this.$scope.cspOrderLine = [{
            //显示
            originAddress: $(".province option:selected")[0].innerHTML + $(".city option:selected")[0].innerHTML + $(".area option:selected")[0].innerHTML + sc.shipAddress,
            destinationAddress: $(".province option:selected")[1].innerHTML + $(".city option:selected")[1].innerHTML + $(".area option:selected")[1].innerHTML + sc.deliverAddress,
            viaListData: this.$scope.viaListData.map((value) => value),
            deliveryTime: sc.deliveryTime,
            arrivalTime: sc.arrivalTime,
            mileage: sc.mileage,
            goodsTypeName: $("#goodsType option:selected").text(),
            // goodsName: $("#goodsId option:selected").text(),
            goodsName: $("#goodsId_value").val(),
            quantityOfGoods: sc.quantityOfGoods,
            goodsUnitStr: $("#goodsNumUnit option:selected").text(),

            //传给数据

            originProvince: sc.shipProvinceCode,
            originCity: sc.shipCityCode,
            originCounty: sc.shipAreaCode,
            originDetails: sc.shipAddress,
            destinationProvince: sc.deliverProvinceCode,
            destinationCity: sc.deliverCityCode,
            destinationCounty: sc.deliverAreaCode,
            destinationDetails: sc.deliverAddress,


            goodsTypeId: sc.goodsTypeId,
            goodsId: sc.goodsId,
            tonnageRange: sc.tonnageRange,

            goodsUnit: sc.goodsUnit,
            quantityOfGoodsTwo: sc.quantityOfGoodsTwo,
            goodsUnitTwo: sc.goodsUnitTwo,
        }];
    }

    /**
     * 添加子线路
     */
    addChildOrderLine = () => {
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return; };
        var settle = $("#reckoner_value").val();
        if (this.$scope.settleId == "" || settle == "") {
            this.$ngBootbox.alert("请选择结算单位");
            return;
        }
        if ($("#goodsId option:selected").text() == "请选择") {
            this.$ngBootbox.alert("请填写货物名称"); return;
        };

        var sc = this.$scope;
        this.$scope.cspChildOrderLine.push({
            index: this.$scope.cspChildOrderLine.length + 1,
            originProvince: sc.shipProvinceCode,
            originCity: sc.shipCityCode,
            originCounty: sc.shipAreaCode,
            originDetails: sc.shipAddress,
            viaListData: this.$scope.viaListData.map((value) => value),
            destinationProvince: sc.deliverProvinceCode,
            destinationCity: sc.deliverCityCode,
            destinationCounty: sc.deliverAreaCode,
            destinationDetails: sc.deliverAddress,
            deliveryTime: $("#dataTime").val(),
            arrivalTime: $("#dataTimeEnd").val(),
            mileage: sc.mileage,
            goodsTypeId: sc.goodsTypeId,
            goodsTypeName: $("#goodsType option:selected").text(),
            goodsId: sc.goodsId,
            // goodsName: $("#goodsId option:selected").text(),
            goodsName: $("#goodsId_value").val(),
            quantityOfGoods: sc.quantityOfGoods,
            goodsUnit: sc.goodsUnit,
            quantityOfGoodsTwo: sc.quantityOfGoodsTwo,
            goodsUnitTwo: sc.goodsUnitTwo,
            tonnageRange: $("#tonRange option:selected").text(),
            receivablePrice: sc.receivablePrice,
            receivablePriceUnitStr: $("#receivablePriceUnit option:selected").text(),
            receivablePriceUnit: sc.receivablePriceUnit,
            receivableTotal: sc.receivableTotal,
            settlementType: sc.settleType,
            settlementTypeStr: $("#settleType option:selected").text(),
            includeTax: sc.includeTax,
            receivableSummary: sc.receivableRemarks,
            settleId: sc.settleId,
            settle: $("#reckoner_value").val(),
            projectId: sc.projectId,
            projectCode: sc.projectCode,
            // projectName: sc.projectName,
            projectName: $("#project_value").val(),
            orderNumber: sc.shipOrderId,
            takeGoodsCompanyId: sc.takeGoodsCompanyId,
            consigneeCompany: sc.takeGoodsCompanyName,
            consignee: sc.consignee,
            consigneePhone: sc.consigneePhone,
            projectMax: sc.projectTotal,
            projectMaxUnit: sc.projectTotalUnit,
            goodsUnitStr: $("#goodsNumUnit option:selected").text(),
            originAddress: $(".province option:selected")[0].innerHTML + $(".city option:selected")[0].innerHTML + $(".area option:selected")[0].innerHTML + sc.shipAddress,
            destinationAddress: $(".province option:selected")[1].innerHTML + $(".city option:selected")[1].innerHTML + $(".area option:selected")[1].innerHTML + sc.deliverAddress,
        });
    }


    /**
     * 删除总线路
     */
    deleteOrderLine = () => {
        // if (this.$scope.childOrderLine.length == 0) {
        //     this.$scope.isClientCanEdit = true;
        // }
        this.$scope.cspOrderLine.length = 0;
        this.$scope.showAddOrderBtn = true;
    }

    /**
     * 删除子线路
     */
    deleteChildOrderLine = (index: number) => {
        this.$scope.cspChildOrderLine.splice(index - 1, 1);
        this.$scope.cspChildOrderLine.forEach((item, index) => {
            this.$scope.cspChildOrderLine[index].index = index + 1;
        });
        this.$scope.showAddChildOrderBtn = true;
    }


    /**
     * 保存/发布订单
     */
    orderSave = () => {
        if (!this.$scope.basicInfo["$valid"]) { this.$ngBootbox.alert("基本信息未填写完整"); return; };
        if (!this.$scope.carInfo["$valid"]) { this.$ngBootbox.alert("车辆信息未填写完整"); return; };
        // if (!this.$scope.orderChildrenForm["$valid"]) { this.$ngBootbox.alert("应收价格信息未填写完整"); return; };
        // if (!this.$scope.orderTotalForm["$valid"]) { this.$ngBootbox.alert("线路货物应收信息未填写完整"); return; };
        if (this.$scope.cspOrderLine.length == 0) { this.$ngBootbox.alert("必须存在一条总线路"); return; };
        if (this.$scope.cspOrderLine.length == 0) { this.$ngBootbox.alert("必须存在一条子线路"); return; };
        if (!this.commonService.timeJudge($("#dataTime").val(), $("#dataTimeEnd").val())) { return };
        this.$scope.enabledSaveBtn = true;
        let orderChild: orderChildVM[] = [];

        this.$scope.cspChildOrderLine.forEach((item) => {

            orderChild.push({
                shipProvince: item.originProvince,
                shipCity: item.originCity,
                shipArea: item.originCounty,
                shipAddress: item.originDetails,
                viaAddressList: item.viaListData,
                deliverProvince: item.destinationProvince,
                deliverCity: item.destinationCity,
                deliverArea: item.destinationCounty,
                deliverAddress: item.destinationDetails,
                shipTime: item.deliveryTime,
                arriveTime: item.arrivalTime,
                mileage: Number(item.mileage) == 0 ? null : Number(item.mileage),
                goodsTypeId: item.goodsTypeId,
                goodsType: item.goodsTypeName,
                goodsId: item.goodsId,
                goodsName: item.goodsName,
                goodsNum: Number(item.quantityOfGoods),
                goodsNumUnit: item.goodsUnit ? item.goodsUnit : "",
                goodsNumTwo: item.quantityOfGoodsTwo,
                goodsNumUnitTwo: item.goodsUnitTwo ? item.goodsUnitTwo : "",
                tonRange: item.tonnageRange,
                receivablePrice: Number(item.receivablePrice) == null ? -1 : Number(item.receivablePrice),
                receivablePriceUnit: item.receivablePriceUnit,
                receivableTotal: Number(item.receivableTotal) == 0 ? null : Number(item.receivableTotal),
                settleType: item.settlementType,
                includeTax: item.includeTax,
                receivableRemarks: item.receivableSummary,
                settleId: item.settleId,
                reckoner: item.settle,
                projectId: item.projectId,
                projectCode: item.projectCode,
                projectName: item.projectName,
                shipOrderId: item.orderNumber,
                takeGoodsCompanyId: item.takeGoodsCompanyId,
                takeGoodsCompany: item.consigneeCompany,
                consignee: item.consignee,
                consigneePhone: item.consigneePhone,
                projectTotal: item.projectMax,
                projectTotalUnit: item.projectMaxUnit

            });
        });
        this.$ngBootbox.confirm("确定发布此单？").then(e => {
            var dispatchOfficerPhone = "";
            this.$scope.dispatchOfficerDropDown.forEach((item, index) => {
                if (item.id === this.$scope.dispatchOfficerId) {
                    dispatchOfficerPhone = item.phoneNumber;
                }
            });

            ///附件类型改换。带后台更改后删除
            var uploaderList = []
            this.uploaderList.forEach((item, index) => {
                uploaderList.push([item.attachmentName, item.attachmentPath]);
            })

            var ol = this.$scope.cspOrderLine[0];
           
            this.clientService.getDetail(this.$scope.clientId).then((res) => {
                var clientCornet = res.data.cornet;
                let contractNumber = res.data.contractNumber;
                this.orderService.add(
                    "",
                    this.$scope.inquiryChildId,
                    this.$scope.clientOrderId,
                    Number(this.$scope.urgency),
                    this.$scope.urgencyUnit,
                    this.$scope.businessOfficerId,
                    this.$scope.customerServiceOfficerId,
                    this.$scope.dispatchOfficerId,
                    this.clientId,
                    clientCornet,
                    this.$scope.consignorId,
                    this.$scope.shipPriceContent,
                    this.$scope.carType,
                    this.$scope.carLength,
                    this.$scope.carriageWay,
                    this.$scope.loadingResult,
                    // this.uploaderList,附件类型改换。带后台更改后删除
                    uploaderList,
                    this.$scope.orderRemarks,
                    orderChild,
                    ol.viaListData.map(value => { return { province: value.province, city: value.city, county: value.county, details: "" } }),
                    ol.originProvince,
                    ol.originCity,
                    ol.originCounty,
                    ol.originDetails,
                    ol.destinationProvince,
                    ol.destinationCity,
                    ol.destinationCounty,
                    ol.destinationDetails,
                    ol.deliveryTime,
                    ol.arrivalTime,
                    Number(ol.mileage) == 0 ? null : Number(ol.mileage),
                    ol.goodsTypeId,
                    ol.goodsTypeName,
                    ol.goodsId,
                    ol.goodsName,
                    //$("#goodsId_value").val(),
                    Number(ol.quantityOfGoods),
                    ol.goodsUnit,
                    ol.quantityOfGoodsTwo,
                    ol.goodsUnitTwo,
                    ol.tonnageRange,
                    $("#dispatchOfficerId option:selected").text(),
                    dispatchOfficerPhone,
                    $("#businessOfficerId option:selected").text(),
                    $("#customerServiceOfficerId option:selected").text(),
                    this.$scope.planOfficer,
                    $("#client_value").val(),
                    this.cspOrderId,
                    contractNumber
                ).then(result => {
                    this.$scope.enabledSaveBtn = false;
                    this.$state.go("app.order.external_order", { name: "external_order" });
                }, error => { this.$scope.enabledSaveBtn = false; });
            }, () => { this.$scope.enabledSaveBtn = false; });

        }, () => { this.$scope.enabledSaveBtn = false; });
    }

    shipBack(): void {
        //   window.history.back();
        this.$state.go("app.order.external_order", { name: "orderAddList" });
    }

    /**
     * 添加中转地
     */
    addVia = () => {
        if (this.$scope.viaProvince == "-1" || this.$scope.viaCity == "-1" || this.$scope.viaArea == "-1") {
            this.$ngBootbox.alert("请填写完整的中转地");
            return;
        }
        let index = this.$scope.viaListData.length + 1;
        //data
        this.$scope.viaListData.push({
            index: index++,
            province: $(".province option:selected")[2].innerHTML,
            city: $(".city option:selected")[2].innerHTML,
            county: $(".area option:selected")[2].innerHTML
        });
    }

    /**
     * 删除中转地
     */
    deleteVia = (index: number) => {
        this.$scope.viaListData.splice(index - 1, 1);
        //重置index

        this.$scope.viaListData.forEach((item, index) => {
            this.$scope.viaListData[index].index = index + 1;
        });
    }
    /**
     * 获取市区下拉
     */
    getProvinceList = () => {
        this.areaService.getCity(this.$scope.shipProvinceCode).then(res => {
            this.$scope.shipCityList = res.data.data;
        });
        this.areaService.getCounty(this.$scope.shipCityCode).then(res => {
            this.$scope.shipAreaList = res.data.data;
        });
        this.areaService.getCity(this.$scope.deliverProvinceCode).then(res => {
            this.$scope.deliverCityList = res.data.data;
        });
        this.areaService.getCounty(this.$scope.deliverCityCode).then(res => {
            this.$scope.deliverAreaList = res.data.data;
        });
    }


    /**
     * 结算单位选中事件
     */
    onsettleSelected = (selected: angucomplete<SettleListItemResponse>) => {
        if (selected) {
            this.$scope.settleId = selected.description.id;
            this.$scope.reckoner = selected.description.name;
            $("#reckoner_value").val(selected.description.name)

        } else {
            // $("#reckoner_value").val("");
            // this.$scope.reckoner = "";
            this.$scope.settleId = "";
        }
    }
    /**
     * 结算单位
     */
    onSettleAutoComplate = (value: string) => {
        if (!this.clientId) { return; };
        return this.settleService.getList(this.clientId, value, "", true, 0, 5).then(result => {
            return result.data.data;
        });
    }


    /**
     * 加载商务专员下拉
     */
    loadBusinessOfficeerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.BusinessAffairs.toString(), 0, -1).then(result => {
            this.$scope.businessOfficerDropDown = result.data.data;
        });
    }

    /**
     * 加载客服专员下拉
     */
    loadCustomerServiceOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.CustomerService.toString(), 0, -1).then(result => {
            this.$scope.customerServiceOfficerDropDown = result.data.data;
        });
    }

    /**
     * 加载调度专员下拉
     */
    loadDispatchOfficerDropDown(): void {
        this.employeeService.getbytype(CommissionerType.Dispatch.toString(), 0, -1).then(result => {
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }

    /**
     * 加载货物类别下拉
     */
    loadGoodsTypeDropDown(): void {
        this.goodsTypeService.getList(this.clientId, "", 0, -1).then(result => {
            this.$scope.goodsTypeDropDown = result.data.data;
            if (!this.$scope.clientId || this.$scope.clientId === "") { this.$scope.goodsDropDown.length = 0; this.$scope.goodsTypeId = ""; this.$scope.tonRangeDropDown.length = 0; };
        });
    }

    /**
     * 加载货物下拉
     */
    // loadGoodsDropDown = (goodsTypeId: string) => {
    //     this.goodsService.getList(this.clientId, "", goodsTypeId, 0, -1).then(result => {
    //         this.$scope.goodsDropDown = result.data.data;
    //          if (!this.$scope.goodsTypeId || this.$scope.goodsTypeId === "") { this.$scope.goodsDropDown.length = 0; this.$scope.goodsId = ""; this.$scope.tonRangeDropDown.length = 0; };
    //     });
    // }

    /**
     * 改变货物类别获取货物
     */
    changeGoodsTypeEvent = () => {

        this.$scope.$broadcast('angucomplete-alt:clearInput', 'goodsId');

    };

    /**
     * 货物名称自动补全事件
     */
    onGoodsNameAutoComplate = (value: string) => {
        // console.log($("#goodsId_value").val());
        var deferred = this.$q.defer();
        if (!this.clientId || !this.$scope.goodsTypeId) {
            // deferred.resolve("well done!");
        } else {
            return this.goodsService.getList(this.clientId, value, this.$scope.goodsTypeId, 0, 30).then(res => {
                return res.data.data;
            })
        }
        return deferred.promise;
        //console.log(this.$q.reject)
    }

    /**
     * 货物名称选中事件
     */
    onGoodsNameSelected = (selected: angucomplete<GoodsListItemResponse>) => {
        if (selected) {
            this.$scope.goodsName = selected.description.name;
            this.$scope.goodsId = selected.description.id;
            $("#goodsId_value").val(selected.description.name);
            //获取吨位范围判断未加
            this.getTonRangeList();
        } else {
            this.$scope.goodsName = "";
            this.$scope.goodsId = "";
        }

    }

    /**
     * 加载结算方下拉
     */
    loadSettleDropDown(): void {
        this.settleService.getList(this.clientId, "", "", true, 0, 1).then(result => {
            this.$scope.settleDropDown = result.data.data;
            if (this.$location.search().name == "add") {
                this.$scope.settleId = result.data.data[0].id;
                $("#reckoner_value").val(result.data.data[0].name)
                this.$scope.reckoner = result.data.data[0].name;
            }
        });
    }

    /**
     * 下拉加载
     */
    loadDropDown(): void {
        //枚举
        this.$scope.urgencyUnitDropDown = this.valueService.getUrgencyUnitList().data;
        this.$scope.goodsNumUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.priceUnitDropDown = this.valueService.getPriceUnitList().data;
        this.$scope.settleTypeDropDown = this.valueService.getSettlementTypeList().data;
        this.$scope.includeTaxDropDown = this.valueService.getLinePriceTypeList().data;
        this.$scope.projectTotalUnitDropDown = this.valueService.getGoodQuantityUnitList().data;
        this.$scope.carTypeDropDown = this.valueService.getCarTypeList().data;
        this.$scope.carLengthDropDown = this.valueService.getCarLengthList().data;
        this.$scope.carriageWayDropDown = this.valueService.getCarriageTypeList().data;
        // this.$scope.goodsNumUnitDropDown = this.valueService.getGoodQuantityUnitList().list;

        //专员加载
        this.loadBusinessOfficeerDropDown();
        this.loadCustomerServiceOfficerDropDown();
        this.loadDispatchOfficerDropDown();
    }

    /**
     * 改变货物获取吨位范围事件
     */
    changeGoodsGetTonRangeList = () => {
        //获取吨位范围判断未加
        this.getTonRangeList();
    }

    /**
     * 吨位范围获取数据
     */
    getTonRangeList = () => {
        this.linePriceService.getList(this.clientId, this.$scope.goodsTypeId, "1", this.$scope.shipProvinceCode, this.$scope.shipCityCode, this.$scope.shipAreaCode, this.$scope.deliverProvinceCode, this.$scope.deliverCityCode, this.$scope.deliverAreaCode, "", this.$scope.goodsId, "", "", 0, -1).then(res => {
            this.$scope.tonRangeDropDown = res.data.data;
            if (!this.$scope.clientId || this.$scope.clientId === "" || this.$scope.goodsId === "" || !this.$scope.goodsId || this.$scope.goodsTypeId === "" || !this.$scope.goodsTypeId) {
                this.$scope.tonRangeDropDown.length = 0;
            }
        });
    }

    /**
     * 改变吨位范围获取应收信息
     */
    changeTonRangeGetReceivableEvent = () => {
        this.linePriceService.getList(this.clientId, this.$scope.goodsTypeId, "1", this.$scope.shipProvinceCode, this.$scope.shipCityCode, this.$scope.shipAreaCode, this.$scope.deliverProvinceCode, this.$scope.deliverCityCode, this.$scope.deliverAreaCode, this.$scope.tonnageRange, this.$scope.goodsId, "", "", 0, -1).then(res => {
            if (res.data.data.length > 0) {
                this.linePriceService.getDetail(res.data.data[0].id).then(res => {
                    this.$scope.receivablePrice = res.data.price;
                    this.$scope.receivablePriceUnit = res.data.priceUnit;
                    this.$scope.settleType = res.data.setleWay;
                    this.$scope.includeTax = res.data.priceType;
                })
            }
        });
    }

    /**
     * 客户单位自动补全事件
     */
    onClientAutoComplate = (value: string) => {
        return this.clientService.getList(value, "", "","", "", "", 0, 5).then(res => {
            return res.data.data;
        });
    }
    /**
     * 选择客户单位事件
     */
    onClientSelected = (data: angucomplete<ClientListItemResponse>) => {
        if (data) {
            this.$scope.consignorId = "";
            this.$scope.goodsTypeId = "";
            this.$scope.goodsDropDown.length = 0;
            this.$scope.projectId = "";
            this.$scope.clientId = data.description.id;
            this.clientId = data.description.id;
            $("#consignor_value").val("");
            $("#reckoner_value").val("");
            this.$scope.reckoner = "";
            this.$scope.settleId = "";
            this.loadGoodsTypeDropDown();

            this.$scope.$broadcast('angucomplete-alt:clearInput', 'goodsId');
            this.loadSettleDropDown();
        } else { return };
    }

    /**
     * 发货计划人自动补全
     */
    onConsignorAutoComplate = (value: string) => {
        if (!this.clientId) { return; };
        return this.customerRepresentativeService.getList(this.clientId, value, "", 0, 5).then(res => {
            return res.data.data;
        })
    }

    /**
     * 发货计划人选中事件
     */
    onConsignorSelected = (selected: angucomplete<CustomerRepresentativeListItemResponse>) => {
        if (selected) {
            this.$scope.consignorPhone = selected.description.phoneNumber;
            this.$scope.consignorId = selected.description.id;
        } else {
            this.$scope.consignorPhone = "";
            this.$scope.consignorId = "";
        }
    }

    /**
     * 工程自动补全时间
     */
    onProjectAutoComplate = (value: string) => {
        if (!this.clientId) { return; };
        return this.projectService.getList(this.clientId, "", value, 0, 5).then(res => {
            return res.data.data;
        })
    }

    /**
     * 选中工程事件
     */
    onProjectSelected = (select: angucomplete<ProjectListItemResponse>) => {
        if (select) {
            this.getProjectData(select.description.id)
        }
    }

    /**
     * 获取工程数据
     */
    getProjectData(id: string): void {
        this.projectService.getDetail(id).then(res => {
            this.$scope.projectTotal = String(res.data.maxValue);
            this.$scope.projectTotalUnit = res.data.maxUnit;
            this.$scope.projectCode = res.data.projectCode;
            this.$scope.takeGoodsCompanyName = res.data.settleName;
            this.$scope.consignee = res.data.settleContact;
            this.$scope.consigneePhone = res.data.settlePhone;
            //获取结算单id
            this.settleService.getList(this.clientId, res.data.settleName, "", true, 0, -1).then(res => {
                this.$scope.takeGoodsCompanyId = res.data.data[0].id;
            });
        });
    }

    /*******************************************************
     **                   附件上传组件                     **
     *******************************************************/
    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number, file?: any) => {
        file.fileId = filePath;
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: ""
        };
        this.uploaderList.push(attachment);
    }


}

angular.module("tms").controller("CspOrderDetailController", ["$scope", "cspOrderService", "areaService", "projectService", "customerRepresentativeService", "clientService", "linePriceService", "employeeService", "goodsTypeService", "routeService", "$http", "inquiryService", "goodsService", "settleService", "valueService", "$ngBootbox", "orderService", "$state", "$location", "commonService", "$q","ossUploaderService", CspOrderDetailController]);